{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Date\n",
    "java.util 包提供了 Date 类来封装当前的日期和时间。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "全部日期和时间信息：Tue Aug 18 15:33:01 UTC 2020\n",
      "年-月-日格式：2020-08-18\n",
      "月/日/年格式：08/18/20\n",
      "HH:MM:SS PM格式（12时制）：03:33:01 PM\n",
      "HH:MM:SS格式（24时制）：15:33:01\n",
      "HH:MM格式（24时制）：15:33"
     ]
    },
    {
     "data": {
      "text/plain": [
       "java.io.PrintStream@1e9d59c4"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// 初始化 Date 对象\n",
    " Date date = new Date();\n",
    "\n",
    " //c的使用  \n",
    "System.out.printf(\"全部日期和时间信息：%tc%n\",date);          \n",
    "//f的使用  \n",
    "System.out.printf(\"年-月-日格式：%tF%n\",date);  \n",
    "//d的使用  \n",
    "System.out.printf(\"月/日/年格式：%tD%n\",date);  \n",
    "//r的使用  \n",
    "System.out.printf(\"HH:MM:SS PM格式（12时制）：%tr%n\",date);  \n",
    "//t的使用  \n",
    "System.out.printf(\"HH:MM:SS格式（24时制）：%tT%n\",date);  \n",
    "//R的使用  \n",
    "System.out.printf(\"HH:MM格式（24时制）：%tR\",date); "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "英文月份简称：Aug\n",
      "本地月份简称：Aug\n",
      "英文月份全称：August\n",
      "本地月份全称：August\n",
      "英文星期的简称：Tue\n",
      "本地星期的简称：Tuesday\n",
      "年的前两位数字（不足两位前面补0）：20\n",
      "年的后两位数字（不足两位前面补0）：20\n",
      "一年中的天数（即年的第几天）：231\n",
      "两位数字的月份（不足两位前面补0）：08\n",
      "两位数字的日（不足两位前面补0）：18\n",
      "月份的日（前面不补0）：18"
     ]
    },
    {
     "data": {
      "text/plain": [
       "java.io.PrintStream@1e9d59c4"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Date date=new Date();                                      \n",
    "//b的使用，月份简称  \n",
    "String str=String.format(Locale.US,\"英文月份简称：%tb\",date);       \n",
    "System.out.println(str);                                                                              \n",
    "System.out.printf(\"本地月份简称：%tb%n\",date);  \n",
    "//B的使用，月份全称  \n",
    "str=String.format(Locale.US,\"英文月份全称：%tB\",date);  \n",
    "System.out.println(str);  \n",
    "System.out.printf(\"本地月份全称：%tB%n\",date);  \n",
    "//a的使用，星期简称  \n",
    "str=String.format(Locale.US,\"英文星期的简称：%ta\",date);  \n",
    "System.out.println(str);  \n",
    "//A的使用，星期全称  \n",
    "System.out.printf(\"本地星期的简称：%tA%n\",date);  \n",
    "//C的使用，年前两位  \n",
    "System.out.printf(\"年的前两位数字（不足两位前面补0）：%tC%n\",date);  \n",
    "//y的使用，年后两位  \n",
    "System.out.printf(\"年的后两位数字（不足两位前面补0）：%ty%n\",date);  \n",
    "//j的使用，一年的天数  \n",
    "System.out.printf(\"一年中的天数（即年的第几天）：%tj%n\",date);  \n",
    "//m的使用，月份  \n",
    "System.out.printf(\"两位数字的月份（不足两位前面补0）：%tm%n\",date);  \n",
    "//d的使用，日（二位，不够补零）  \n",
    "System.out.printf(\"两位数字的日（不足两位前面补0）：%td%n\",date);  \n",
    "//e的使用，日（一位不补零）  \n",
    "System.out.printf(\"月份的日（前面不补0）：%te\",date); "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import java.util.*;\n",
    "import java.text.*;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3030-9-8\n",
      "3030-9-8 Parses as Wed Sep 08 00:00:00 UTC 3030\n"
     ]
    }
   ],
   "source": [
    "Scanner sc = new Scanner(System.in);\n",
    "String input = sc.next();\n",
    "\n",
    "SimpleDateFormat ft = new SimpleDateFormat (\"yyyy-MM-dd\"); \n",
    "System.out.print(input + \" Parses as \"); \n",
    "Date t; \n",
    "try { \n",
    "    t = ft.parse(input); \n",
    "    System.out.println(t); \n",
    "} catch (ParseException e) { \n",
    "    System.out.println(\"Unparseable using \" + ft); \n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 测量运行时间（以毫秒为单位）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tue Aug 18 15:40:53 UTC 2020\n",
      "\n",
      "Tue Aug 18 15:40:56 UTC 2020\n",
      "\n",
      "Difference is : 3002\n"
     ]
    }
   ],
   "source": [
    "try {\n",
    "     long start = System.currentTimeMillis( );\n",
    "     System.out.println(new Date( ) + \"\\n\");\n",
    "     Thread.sleep(5*60*10);\n",
    "     System.out.println(new Date( ) + \"\\n\");\n",
    "     long end = System.currentTimeMillis( );\n",
    "     long diff = end - start;\n",
    "     System.out.println(\"Difference is : \" + diff);\n",
    "  } catch (Exception e) {\n",
    "     System.out.println(\"Got an exception!\");\n",
    "  }"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Calendar\n",
    "Calendar类的功能要比Date类强大很多，而且在实现方式上也比Date类要复杂一些。\n",
    "\n",
    "Calendar类是一个抽象类，在实际使用时实现特定的子类的对象，创建对象的过程对程序员来说是透明的，只需要使用getInstance方法创建即可。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020 8 18 15 47 42 3\n"
     ]
    }
   ],
   "source": [
    "Calendar c1 = Calendar.getInstance();//默认是当前日期\n",
    "// 获得年份\n",
    "int year = c1.get(Calendar.YEAR);\n",
    "// 获得月份\n",
    "int month = c1.get(Calendar.MONTH) + 1;\n",
    "// 获得日期\n",
    "int date = c1.get(Calendar.DATE);\n",
    "// 获得小时\n",
    "int hour = c1.get(Calendar.HOUR_OF_DAY);\n",
    "// 获得分钟\n",
    "int minute = c1.get(Calendar.MINUTE);\n",
    "// 获得秒\n",
    "int second = c1.get(Calendar.SECOND);\n",
    "// 获得星期几（注意（这个与Date类是不同的）：1代表星期日、2代表星期1、3代表星期二，以此类推）\n",
    "int day = c1.get(Calendar.DAY_OF_WEEK);\n",
    "\n",
    "String space = \" \";\n",
    "\n",
    "System.out.println(year+space+month+space+date+space+hour+space+minute+space+second+space+day);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# GregorianCalendar\n",
    "Calendar类实现了公历日历，GregorianCalendar是Calendar类的一个具体实现。\n",
    "\n",
    "Calender的月份是从0开始的，但日期和年份是从1开始的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Date: Aug 18 2020\n",
      "Time: 3:49:11\n",
      "当前年份是闰年\n"
     ]
    }
   ],
   "source": [
    "String months[] = {\n",
    "\"Jan\", \"Feb\", \"Mar\", \"Apr\",\n",
    "\"May\", \"Jun\", \"Jul\", \"Aug\",\n",
    "\"Sep\", \"Oct\", \"Nov\", \"Dec\"};\n",
    "\n",
    "int year;\n",
    "// 初始化 Gregorian 日历\n",
    "// 使用当前时间和日期\n",
    "// 默认为本地时间和时区\n",
    "GregorianCalendar gcalendar = new GregorianCalendar();\n",
    "// 显示当前时间和日期的信息\n",
    "System.out.print(\"Date: \");\n",
    "System.out.print(months[gcalendar.get(Calendar.MONTH)]);\n",
    "System.out.print(\" \" + gcalendar.get(Calendar.DATE) + \" \");\n",
    "System.out.println(year = gcalendar.get(Calendar.YEAR));\n",
    "System.out.print(\"Time: \");\n",
    "System.out.print(gcalendar.get(Calendar.HOUR) + \":\");\n",
    "System.out.print(gcalendar.get(Calendar.MINUTE) + \":\");\n",
    "System.out.println(gcalendar.get(Calendar.SECOND));\n",
    "\n",
    "// 测试当前年份是否为闰年\n",
    "if(gcalendar.isLeapYear(year)) {\n",
    " System.out.println(\"当前年份是闰年\");\n",
    "}\n",
    "else {\n",
    " System.out.println(\"当前年份不是闰年\");\n",
    "}"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Java",
   "language": "java",
   "name": "java"
  },
  "language_info": {
   "codemirror_mode": "java",
   "file_extension": ".jshell",
   "mimetype": "text/x-java-source",
   "name": "Java",
   "pygments_lexer": "java",
   "version": "11.0.8+10-post-Ubuntu-0ubuntu120.04"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
